var isDoMenuVerified = false;
/**
 * 菜单管理
 */
$(function() {
	$("#submit").hide();
    getMemu(null);
    $("#menuDiv").height($("body").height() - 140);
    $("#addNode").click(function() {
    	noReadOnly();
    	$("#nameError").html("");
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getSelectedNodes();
        if (nodes.length > 0) {
        	$("#menuForm")[0].reset();
        	$("#id").val("");
        	$("#url").val("");
        	$("#name").val("");
            $("#supId").val(nodes[0].supId);
            var parentNode = nodes[0].getParentNode();
            if (nodes[0].supId == 0 || parentNode == null) {
                $("#supMenuName").val("无");
            } else {
                $("#supMenuName").val(parentNode.name);
            }
            $("#submit").show();
        } else {
        	alertify.warning('请先选择要操作的菜单项！');
            return;
        }
    });

    $("#addChildren").click(function() {
    	noReadOnly();
    	$("#nameError").html("");
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getSelectedNodes();
        if (nodes.length > 0) { 
        	$('#menuForm')[0].reset();
        	$("#id").val("");
        	$("#url").val("");
        	$("#name").val("");
            $("#supId").val(nodes[0].id);
            $("#supMenuName").val(nodes[0].name);
        	$("#submit").show();
        } else {
        	alertify.warning('请先选择要操作的菜单项');
            return;
        }
    });
    $("#editNode").click(function() {
    	noReadOnly();
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getSelectedNodes();
        if (nodes.length > 0) {
        	$('#menuForm')[0].reset();
        	var parentNode = nodes[0].getParentNode();
            $("#id").val(nodes[0].id);
            $("#name").val(nodes[0].name);
            $("#url").val(nodes[0].newUrl);
            $("#supId").val(nodes[0].supId);
            if (nodes[0].supId == 0) {
                $("#supMenuName").val("无");
            } else {
                $("#supMenuName").val(parentNode.name);
            }
        	$("#submit").show();
        } else {
        	alertify.warning('请先选择要操作的菜单项！');
            return;
        }
    });
    $("#removeNode").click(function() {  	
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getSelectedNodes();
        if(nodes[0].isParent){
        	alertify.warning('该菜单下有子菜单，请先删除子菜单');
        	return;
        }
        alertify.confirm("删除","确定要删除  "+nodes[0].name+"  吗？",function(){
        	$.ajax({
                type: 'POST',
                url: '/sys/modifyStatus.do',
                data: { ids: nodes[0].id, status: '0',name: nodes[0].name },
                dataType: 'json',
                success: function(iRet) {
                    if (iRet.code == "102") {
                    	alertify.success('删除成功');
                    	$('#menuForm')[0].reset();	
                    	treeObj.removeNode(nodes[0]);
                    } else if (iRet.code == "202") {
                    	alertify.warning('删除发生意外错误！');
                    }             
                },
                error: function() {
                	alertify.error('系统出错,请联系管理员！');
                }
            });
        },function(){
        	
        });
    });

    $("#submit").click(function() {
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getSelectedNodes();
    	var data = getFormValue('menuForm');
    	if( data.id == "" || data.id == null){
    		url = "/sys/insert.do";    		
    	}else{
    		url = "/sys/update.do";
    	}	
    	isDoMenuVerified = false;
    	$("#nameError").html("");
    	doMenuVerified();
    	if(isDoMenuVerified){
    		$.ajax({
    			type:"post",
    			url:url,
    			data:data,
    			datatype:"json",
    			success:function(res){
    				if(res.code == "101"){
    					alertify.success('修改成功！');
    					treeObj.cancelSelectedNode();
    					$('#menuForm')[0].reset();
    					getMemu(nodes);
    		            $("#id").val("");
    		            $("#name").val("");
    		            $("#url").val("");
    		            $("#supId").val("");
    		            $("#supMenuName").val("");
    				}else if(res.code == "100"){
    					alertify.success('添加成功');
    					treeObj.cancelSelectedNode(); 
    					$('#menuForm')[0].reset();
    					if(data.supId == 0){ //添加根节点的同级菜单
    						nodes = null;
    		    		}else{
    		    			var pNode = treeObj.getNodeByParam("id",data.supId, null);
    		    			if(pNode == nodes[0]){ //添加子级菜单
    		    				nodes = nodes;
    		    			}else{ //添加非根节点的同级菜单
    		    				nodes = pNode;
    		    			}
    		    		}
    					getMemu(nodes);
    				}else if(res.code == "201"){
    					alertify.warning('已存在该节点，请重试');
    					return;
    				}else{
    					alertify.warning('操作失败，请重试！');
    					return;
    				}	 
    				$("#submit").hide();
    			},
    			error:function(){
    				alertify.error('网络传输错误，请检查网络再试！');
    			},
    		});
    	}
    });
})
/**
 * 生成菜单树
 */
function getMemu(treeNode){
	$("#nameError").html("");
    $.ajax({
        type: "Post",
        url: "/sys/getMenu.do",
        data: { status: 1 },
        dataType: "json",
        success: function(res) {
            if (res.length > 0) {
                for (i = 0; i < res.length; i++) {
                	res[i].newUrl = res[i].url;
                	res[i].url = "";
                    res[i].id = res[i].id;
                    res[i].name = res[i].name;
                    res[i].pId = res[i].supId;
                }
            }
            var setting = {
            		
                view: {
                    selectedMulti: false
                },
                edit: {
                    enable: true,
                    showRemoveBtn: false,
                    showRenameBtn: false
                },
                data: {
                    keep: {
                        parent: false,
                        leaf: true
                    },
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: zTreeOnClick,
                }
            };
            $.fn.zTree.init($("#menuTree"), setting, res);
            
            //展开指定节点
            if(treeNode != null){
            	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
            	treeObj.selectNode(treeNode[0],true);				//指定选中ID的节点  
            	treeObj.expandNode(treeNode[0], true, true, true);	//指定选中ID节点展开
            	treeObj.cancelSelectedNode(treeNode[0]);
            }else{
            }
        }
    });
}
function zTreeOnClick(event, treeId, treeNode) {
	readOnly();
    $("#nameError").html("");
    $("#submit").hide();
    setFormValue(treeNode);
    $("#url").val(treeNode.newUrl);
    var parentNode = treeNode.getParentNode();
    if (parentNode) {
        $("#supMenuName").val(parentNode.name);
    } else {
        $("#supMenuName").val("无");
    }
};
function doMenuVerified(){
	if($("#name").val()==null||$("#name").val()==""){
		$("#nameError").html("名称不能为空!");
		return false;
	}
	isDoMenuVerified = true;
}

function noReadOnly(){
	$("#name").prop("readonly",false);
	$("#url").prop("readonly",false);
	$("#remark").prop("readonly",false);
}
function readOnly(){
	$("#name").prop("readonly",true);
	$("#url").prop("readonly",true);
	$("#remark").prop("readonly",true);
}